/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/


#include "MkEvent/MkEvent.h"
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include "MkUtil/MkLog.h"
#include "MkEvent/MkPipe.h"
#include <signal.h>

CMkTimer g_Timer;
int main(int argc, char ** argv)
{

#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#else
    signal(SIGPIPE, SIG_IGN);  //ignore sigpipe
#endif
    MkLogChangeLevel(LOG_DEBUG);
    CMkEventPool Pool;
    Pool.Start();
    MkDebugLog("main thread\n");
    CMkPipe Pipe(Pool.GetFreeEvent());
    CMkPipe *pPipe = &Pipe;
    signal(SIGINT, [](int sig) {
        if (SIGINT == sig) {
            MkDebugLog("recv exit sign\n");
            g_Timer.LoopBreak();
            MkDebugLog("loop break\n");
        }
    });// set exit  signal
    Pipe.Start([]() {
        MkDebugLog("pip actived\n");
    });
    MkString PipeTimer;
    g_Timer.AddTimer([pPipe]() {
        pPipe->Active();
        MkDebugLog("pipe send active\n");
    }, 1000, PipeTimer);
    g_Timer.Dispatch();
    Pool.Stop();
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}